package com.hgk.wanwan.main.fragment.persentfragment.alarmclock;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BootAlarm extends Activity {
	private Calendar btAlarmCalendar;
	private DatabaseHelper dbHelper;
	private AlarmManager mAlarm;// �������
	String time = "";
	String year = "";
	String month = "";
	String day = "";
	String hour = "";
	String minute = "";
	int myear = 0, mmonth = 0, mday = 0, mhour = 0, mminute = 0;
	Intent intent;
	PendingIntent pendingIntent;

	long tempTime = 0, beginTime = 0;
	int cursorNum = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		System.out.println("BootAlarm--->booting");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy��MM��dd��HHʱmm");

		dbHelper = new DatabaseHelper(BootAlarm.this, "ideal.sql");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query("user", null, null, null, null, null, null);
		btAlarmCalendar = Calendar.getInstance();
		//�������ʱ��С�ڵ�ǰ�ģ���ô�Ͱ�����ȡ��
		Date currentTime = new Date();
		while (cursor.moveToNext()) {
			if (cursor.getLong(cursor.getColumnIndex("isalarm")) == 1) {				
				try {
					beginTime = sdf.parse(
							cursor.getString(cursor.getColumnIndex("mtime")))
							.getTime();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(beginTime<=currentTime.getTime()){
					continue;
				}
				cursorNum++;
				if (cursorNum == 1) {
					tempTime = beginTime;
					AlarmActivity.Alarming_index = cursor.getInt(0);
				} else {
					if (beginTime <= tempTime) {
						tempTime = beginTime;
						AlarmActivity.Alarming_index = cursor.getInt(0);
					}
				}
				System.out.println("NoteActivity.Alarming_index-->"+AlarmActivity.Alarming_index);
				btAlarmCalendar = Calendar.getInstance();
				time = cursor.getString(cursor.getColumnIndex("mtime"));
				year = time.substring(0, time.indexOf("��"));
				myear = Integer.valueOf(year).intValue();
				month = time
						.substring(time.indexOf("��") + 1, time.indexOf("��"));
				mmonth = Integer.valueOf(month).intValue() - 1;
				day = time.substring(time.indexOf("��") + 1, time.indexOf("��"));
				mday = Integer.valueOf(day).intValue();
				hour = time.substring(time.indexOf("��") + 1, time.indexOf("ʱ"));
				mhour = Integer.valueOf(hour).intValue();
				minute = time.substring(time.indexOf("ʱ") + 1,
						time.indexOf("��"));
				mminute = Integer.valueOf(minute).intValue();
				System.out.println(myear + "��" + mmonth + "��" + mday + "��"
						+ mhour + "ʱ" + mminute);
				btAlarmCalendar.set(Calendar.YEAR, myear);
				btAlarmCalendar.set(Calendar.MONTH, mmonth);
				btAlarmCalendar.set(Calendar.DAY_OF_MONTH, mday);
				btAlarmCalendar.set(Calendar.HOUR_OF_DAY, mhour);
				btAlarmCalendar.set(Calendar.MINUTE, mminute);
				btAlarmCalendar.set(Calendar.SECOND, 0);
				btAlarmCalendar.set(Calendar.MILLISECOND, 0);
				mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
				intent = new Intent(BootAlarm.this, AlarmReceiver.class);
				pendingIntent = PendingIntent.getBroadcast(BootAlarm.this,
						cursor.getInt(0), intent, 0);// �ڶ�������Ϊ���ͬ�����Ψһ��ʶ
				/* �������� */
				mAlarm.set(AlarmManager.RTC_WAKEUP,
						btAlarmCalendar.getTimeInMillis(), pendingIntent);
			}
		}
		db.close();
		Intent intent = new Intent(BootAlarm.this, AlarmActivity.class);
		startActivity(intent);
		BootAlarm.this.finish();
	}

}
